package controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import model.ModelFacade;

import battleship.ConfigureGame;
import battleship.Observer;
import battleship.Subject;
//este anuntat de mf cand se termina jocul
public class Controller implements ControllerInterface, Observer 
{
	private ModelFacade mf = new ModelFacade();
	private boolean play = true;
	
	public Controller()
	{
		this.submit();
	}
	
	public void submit()
	{
		this.mf.addObserver(this);
	}
	
	@Override
	public void play() 
	{
		while(play)
			mf.fire();
	}

	@Override
	public void positionShips() 
	{
		this.mf.positionShips();
	}

	@Override
	public void initializeGame() 
	{
		String s;
		int i,id;
		try
		{
			BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("conf_battleship.txt")));
			ConfigureGame.LOWER_BOUNDARY = Integer.parseInt(file.readLine());
			ConfigureGame.HIGHER_BOUNDARY = Integer.parseInt(file.readLine());
			ConfigureGame.NR_SHIPS = Integer.parseInt(file.readLine());
			ConfigureGame.SHIP_NR_BLOCKS = new int [ConfigureGame.NR_SHIPS];
			for(i=0;i<ConfigureGame.NR_SHIPS;i++)
			{
				s=file.readLine();
				StringTokenizer st = new StringTokenizer(s);
				id=Integer.parseInt(st.nextToken());
				ConfigureGame.SHIP_NR_BLOCKS[id] = Integer.parseInt(st.nextToken());
			}
			
		}catch(IOException e){
			
		}
		
	}

	@Override
	public void update(Subject subject, Object data) 
	{
		System.out.println(" SURRENDER !!!\n GAME OVER !!! ");
		this.play = false;
	}

}
